jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.deployedService.DeployedService", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	
        	plugin.deployedServiceTableCacheId = "SPM_DEPLOYED_SERVICE_TABLE_CACHE";
        	
        	plugin.prototype.serviceId;
			plugin.prototype.nameSuffix = "";
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function(nameSuffix, serviceId) {
				this.nameSuffix = nameSuffix;
                this.serviceId=serviceId;
				
				var titleLabel = this.getDescendantOfName("titleLabel");
				titleLabel.setName(titleLabel.getName() + nameSuffix);
				var deployedServiceTable = this.getDescendantOfName("deployedServiceTable");
				deployedServiceTable.setName(deployedServiceTable.getName() + nameSuffix);
				
                tibco.admin.monitormanage.general.PollingHandler.register(tibco.admin.monitormanage.general.PollingHandler.GENERAL_POLLING_HANDLER_ID, this, this.updateComponent, tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceScreenId);
                tibco.admin.monitormanage.general.RefreshHandler.register(this, this.updateComponent, tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceScreenId);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.deployment.Deployment.getServiceIdSubject, this.handleGetServiceId, this);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.selectedTimeframeSubject, this.timeframeSelected, this);
				this.callGetDeployedNodesForService();
			}
			
			plugin.showServiceInstanceDetail = function(nodeIdVal, serviceIdVal) {
				var serviceInstanceIdVal = serviceIdVal + "/" + nodeIdVal;
				var paramArray = { nodeId:nodeIdVal, serviceInstanceId:serviceInstanceIdVal };
        		tibco.admin.monitormanage.general.NavigationHandler.publishMainPageNavigationMessage(tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInstanceScreenId, paramArray); 
			}
			
			plugin.prototype.updateComponent = function() {
				this.callGetDeployedNodesForService();
			}
			
			plugin.prototype.handleGetServiceId = function(subject, message, tableObj) {
				tableObj.serviceId = message.serviceId;
				tableObj.callGetDeployedNodesForService();
			}
			
			plugin.prototype.timeframeSelected = function(subject, message, tableObj) {
            	var timeframeId = message.selectedTimeframe;
            	tableObj.callGetDeployedNodesForService(timeframeId);
            }
			
			//call this method to begin the service call (eg.service.callgetDeployedNodesForService();)
    		plugin.prototype.callGetDeployedNodesForService = function(timeframeId) {
    			var dataPath = "tibco/admin/monitormanage/uicomponents/table/deployedService/data/deployedServiceData.xml";
    			var rulesPath = "tibco/admin/monitormanage/uicomponents/table/deployedService/rules/deployedServiceRules.xml";
    			if (tibco.admin.Main.getServer().getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(this.getNamespace(), rulesPath, "getDeployedNodesForService");
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL(dataPath);
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), rulesPath), "getDeployedNodesForService", null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), dataPath));
                }
                
                objService.deployedServiceObj = this;
                
                objService.interfacesObj = this;
                objService.serviceId = this.serviceId;
                if(!timeframeId)
				{ timeframeId = tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeId(); }
				objService.timeframeId = timeframeId;
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("status");
				metricsList.add("faults");
				metricsList.add("successes");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetDeployedNodesForServiceSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetDeployedNodesForServiceError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetDeployedNodesForServiceInvalid);
      			objService.doCall();
    		};

    		plugin.prototype.ongetDeployedNodesForServiceSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//jsx3.log("Success","The service call was successful.");
      			var deployedServiceObj = objEvent.target.deployedServiceObj;
      			
      			var cacheDoc = tibco.admin.Main.getServer().Cache.getDocument(plugin.deployedServiceTableCacheId);
      			var nodeRecords = cacheDoc.getChildNodes();
      			for(i = 0; i < nodeRecords.size(); i++)
      			{
      				var nodeRecord = nodeRecords.get(i);
   					var faults = nodeRecord.getAttribute("faults");
   					var successes = nodeRecord.getAttribute("successes");
   					var faultSuccessRatio = faults + "/" + successes;
   					var faultSuccessRatioValue = faults/successes;
   					nodeRecord.setAttribute("faultsSuccesses",faultSuccessRatio);
   					nodeRecord.setAttribute("faultSuccessRatioValue",faultSuccessRatioValue);
   					var hits = (faults - 0) + (successes - 0);
   					nodeRecord.setAttribute("hits", hits);
   					
   					// add the service id to the record
   					// TODO: correctly pass the service key
   					nodeRecord.setAttribute("serviceKey", deployedServiceObj.serviceId);
      			}
      			
      			var deployedServiceTable = deployedServiceObj.getDescendantOfName("deployedServiceTable" + deployedServiceObj.nameSuffix);
      			deployedServiceTable.repaintData();
      			var server = tibco.admin.Main.getServer();
      			var tableLabel = deployedServiceObj.getDescendantOfName("titleLabel" + deployedServiceObj.nameSuffix);
      			var tableLabelText = server.getDynamicProperty("@MM@general@serviceDeployedToLabel1") + nodeRecords.size() + server.getDynamicProperty("@MM@general@serviceDeployedToLabel2");
      			tableLabel.setText(tableLabelText);
      			tableLabel.repaint();
    		};

    		plugin.prototype.ongetDeployedNodesForServiceError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			//jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetDeployedNodesForServiceInvalid = function(objEvent) {
      			//jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};

        });